﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Forecast.Logica.Enumeradores
Imports Forecast.Logica.Servicios
Imports Forecast.Logica.Modelos

Public Class Listado
    Inherits System.Web.UI.Page
    Private Shared _ServForecast As ServForecast
    Private Shared _servEntidades As ServEntidades
    Private Shared _servServProduct As ServProduct

    Private Shared _servUsuarios As ServUsuarios
    Private Shared _servClientes As ServClientes
    Private Shared _servSegmentoPL As ServSegmentPL
    Private Shared _ServPeriod As ServPeriod
    Private Shared _servUOM As ServUOM

    Public Usuario As Usuario
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Usuario = Session("usuario")
        Dim IdEntidad As Integer

        If (Usuario Is Nothing) Then
            Response.Redirect("~/IniciarSesion.aspx")
        End If

        If Not Page.IsPostBack Then
            CargarEntidadesUsuarios()
            TBFiltro.Visible = False


        End If
        If Session("ActualizarListado") = True Then
            Session("ActualizarListado") = False

            If Session("IdEntidad") IsNot Nothing Then
                IdEntidad = Session("IdEntidad")
            Else
                IdEntidad = Usuario.IdEntityF
            End If
            DrEntidadLegalUsuario2.SelectedValue = IdEntidad

            CargarCombos(1)
            CargarListado()

            'MostrarListado()
        End If


        '_ServForecast = New ServForecast()
        'Dim objDT As System.Data.DataTable
        'Dim TBProductoDeForecast = _ServForecast.Search_ForeCastDeUsuario(1, 2)  '# DEJAR PARAMETRIZADO
        'Dim objDR As System.Data.DataRow
        'Dim MForeast(5, 14) As String
        'Dim codCliente, IdProductLine As Integer
        'Dim I As Integer
        'Dim H As Integer

        'Try

        '    If TBProductoDeForecast.Rows.Count > 0 Then
        '        For k = 0 To TBProductoDeForecast.Rows.Count - 1 Step 1
        '            If codCliente <> TBProductoDeForecast.Rows(k).Item("IdCustomer") Then
        '                codCliente = TBProductoDeForecast.Rows(k).Item("IdCustomer")
        '                H = 0
        '                MForeast(I, H) = TBProductoDeForecast.Rows(k).Item("IdCustomer")
        '                H = H + 1
        '                MForeast(I, H) = TBProductoDeForecast.Rows(k).Item("Codigo")
        '                If k > 0 Then : I = I + 1 : End If
        '            End If
        '            H = H + 1
        '            MForeast(I, H) = TBProductoDeForecast.Rows(k).Item("ValuesSP")
        '        Next
        '    End If

        '    Dim table As DataTable = New DataTable("Tabla1")

        '    table.Columns.Add(New DataColumn("IdCustomer", GetType(String)))
        '    table.Columns.Add(New DataColumn("Cliente", GetType(String)))
        '    table.Columns.Add(New DataColumn("IdSegment", GetType(String)))
        '    table.Columns.Add(New DataColumn("Segmento", GetType(String)))
        '    table.Columns.Add(New DataColumn("IdProductLine", GetType(String)))
        '    table.Columns.Add(New DataColumn("ProdutLine", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes1", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes2", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes3", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes4", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes5", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes6", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes7", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes8", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes9", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes10", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes11", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes12", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes13", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes14", GetType(String)))
        '    table.Columns.Add(New DataColumn("Mes15", GetType(String)))

        '    If TBProductoDeForecast.Rows.Count > 0 Then
        '        'Ahora creamos filas para que la tabla tenga datos
        '        Dim row As DataRow = table.NewRow()
        '        For k = 0 To TBProductoDeForecast.Rows.Count - 1 Step 1
        '            If codCliente <> TBProductoDeForecast.Rows(k).Item("IdCustomer") Or IdProductLine <> TBProductoDeForecast.Rows(k).Item("IdProductLine") Then
        '                codCliente = TBProductoDeForecast.Rows(k).Item("IdCustomer")
        '                IdProductLine = TBProductoDeForecast.Rows(k).Item("IdProductLine")

        '                If k > 0 Then
        '                    table.Rows.Add(row)
        '                End If
        '                row = table.NewRow()
        '                I = 0
        '                row("IdCustomer") = TBProductoDeForecast.Rows(k).Item("IdCustomer")
        '                row("Cliente") = TBProductoDeForecast.Rows(k).Item("Cliente")
        '                row("IdSegment") = TBProductoDeForecast.Rows(k).Item("IdSegment")
        '                row("Segmento") = TBProductoDeForecast.Rows(k).Item("DesSegment")
        '                row("IdProductLine") = TBProductoDeForecast.Rows(k).Item("IdProductLine")
        '                row("ProdutLine") = TBProductoDeForecast.Rows(k).Item("DesProductLine")
        '            End If
        '            GridView1.Columns(I + 7).HeaderText = TBProductoDeForecast.Rows(k).Item("DesPeriodo")

        '            If I = 0 Then : row("Mes1") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 1 Then : row("Mes2") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 2 Then : row("Mes3") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 3 Then : row("Mes4") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 4 Then : row("Mes5") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 5 Then : row("Mes6") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 6 Then : row("Mes7") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 7 Then : row("Mes8") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 8 Then : row("Mes9") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 9 Then : row("Mes10") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 10 Then : row("Mes11") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 11 Then : row("Mes12") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 12 Then : row("Mes13") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 13 Then : row("Mes14") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
        '            If I = 14 Then : row("Mes15") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If

        '            I = I + 1
        '        Next
        '        table.Rows.Add(row)
        '    End If
        '    Session("TBListado") = table
        '    GridView1.DataSource = table
        '    GridView1.DataBind()

        'Catch ex As Exception

        'End Try


    End Sub
    Public Function MostrarListado()
        If Session("MostrarListado") = True Then
            TblListadoFore.Visible = True
            'ctrlAgregarForecast1.upAgregarGasto.visible = False
            ' Panel2.Visible = False
        Else
            TblListadoFore.Visible = False
            'Panel2.Visible = True
        End If
    End Function
    Public Function CargarListado()
        _ServForecast = New ServForecast()
        Dim Usuario = Session("usuario")
        Dim IdEntidad As Integer
        If Session("IdEntidad") IsNot Nothing Then
            IdEntidad = Session("IdEntidad")
        Else
            IdEntidad = Usuario.IdEntityF
        End If

        Dim _BusForecast = CargaParamBusqueda()

        Dim TBProductoDeForecast = _ServForecast.Search_ForeCastDeUsuario(Usuario.IdUsuario, IdEntidad, _BusForecast)
        Dim codCliente, IdProductLine As Integer
        Dim I As Integer
        Dim H As Integer

        'Try
        Dim table As DataTable = New DataTable("Tabla1")

        table.Columns.Add(New DataColumn("IdCustomer", GetType(String)))
        table.Columns.Add(New DataColumn("Cliente", GetType(String)))
        table.Columns.Add(New DataColumn("IdSegment", GetType(String)))
        table.Columns.Add(New DataColumn("Segmento", GetType(String)))
        table.Columns.Add(New DataColumn("IdProductLine", GetType(String)))
        table.Columns.Add(New DataColumn("ProdutLine", GetType(String)))
        table.Columns.Add(New DataColumn("Mes1", GetType(String)))
        table.Columns.Add(New DataColumn("Mes2", GetType(String)))
        table.Columns.Add(New DataColumn("Mes3", GetType(String)))
        table.Columns.Add(New DataColumn("Mes4", GetType(String)))
        table.Columns.Add(New DataColumn("Mes5", GetType(String)))
        table.Columns.Add(New DataColumn("Mes6", GetType(String)))
        table.Columns.Add(New DataColumn("Mes7", GetType(String)))
        table.Columns.Add(New DataColumn("Mes8", GetType(String)))
        table.Columns.Add(New DataColumn("Mes9", GetType(String)))
        table.Columns.Add(New DataColumn("Mes10", GetType(String)))
        table.Columns.Add(New DataColumn("Mes11", GetType(String)))
        table.Columns.Add(New DataColumn("Mes12", GetType(String)))
        table.Columns.Add(New DataColumn("Mes13", GetType(String)))
        table.Columns.Add(New DataColumn("Mes14", GetType(String)))
        table.Columns.Add(New DataColumn("Mes15", GetType(String)))

        If TBProductoDeForecast.Rows.Count > 0 Then
            'Ahora creamos filas para que la tabla tenga datos
            Dim row As DataRow = table.NewRow()
            For k = 0 To TBProductoDeForecast.Rows.Count - 1 Step 1
                If codCliente <> TBProductoDeForecast.Rows(k).Item("IdCustomer") Or IdProductLine <> TBProductoDeForecast.Rows(k).Item("IdProductLine") Then
                    codCliente = TBProductoDeForecast.Rows(k).Item("IdCustomer")
                    IdProductLine = TBProductoDeForecast.Rows(k).Item("IdProductLine")

                    If k > 0 Then
                        table.Rows.Add(row)
                    End If
                    row = table.NewRow()
                    I = 0
                    row("IdCustomer") = TBProductoDeForecast.Rows(k).Item("IdCustomer")
                    row("Cliente") = TBProductoDeForecast.Rows(k).Item("Cliente")
                    row("IdSegment") = TBProductoDeForecast.Rows(k).Item("IdSegment")
                    row("Segmento") = TBProductoDeForecast.Rows(k).Item("DesSegment")
                    row("IdProductLine") = TBProductoDeForecast.Rows(k).Item("IdProductLine")
                    row("ProdutLine") = TBProductoDeForecast.Rows(k).Item("DesProductLine")
                End If
                GridView1.Columns(I + 7).HeaderText = TBProductoDeForecast.Rows(k).Item("DesPeriodo")

                If I = 0 Then : row("Mes1") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 1 Then : row("Mes2") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 2 Then : row("Mes3") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 3 Then : row("Mes4") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 4 Then : row("Mes5") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 5 Then : row("Mes6") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 6 Then : row("Mes7") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 7 Then : row("Mes8") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 8 Then : row("Mes9") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 9 Then : row("Mes10") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 10 Then : row("Mes11") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 11 Then : row("Mes12") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 12 Then : row("Mes13") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 13 Then : row("Mes14") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If
                If I = 14 Then : row("Mes15") = TBProductoDeForecast.Rows(k).Item("ValuesSP") : End If

                I = I + 1
            Next
            table.Rows.Add(row)
        End If
        Session("TBListado") = table
        GridView1.DataSource = table
        GridView1.DataBind()
        'Catch ex As Exception

        'End Try

    End Function

    Protected Sub btnAgregarGasto_Click(sender As Object, e As EventArgs) Handles LoadLocalDialog.Click
        'Dim _BusForecast = CargaParamBusqueda()
        'ctrlAgregarForecast1.CargarProductoDeForecast(_BusForecast)
        'ctrlAgregarForecast1.Mostrar()

    End Sub

    Private Function CargaParamBusqueda(IdCustomer As Integer, IdSegmento As Integer, IdProductLine As Integer) As BusForecast

        Dim BusForecast = New BusForecast()
        BusForecast.IdCustomer = IdCustomer 'DrClientesB.SelectedValue
        BusForecast.IdUOM = 0 'DrUOMB.SelectedValue
        'If DrProductoB.SelectedValue <> "" Then
        '    BusForecast.IdProduct = CInt(DrProductoB.SelectedValue)
        'End If
        BusForecast.IdSegmento = IdSegmento 'DrSegmentoPLB.SelectedValue
        BusForecast.IdProductLine = IdProductLine 'DrProductLineB.SelectedValue
        BusForecast.CodCustomer = "" 'TxtCodClienteB.Text
        Return BusForecast

    End Function

    Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "Select" Then
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim IdCustomer, IdSegment, IdProductLine As Integer
            IdCustomer = Session("TBListado").rows(index).Item("IdCustomer").ToString
            IdSegment = Session("TBListado").rows(index).Item("IdSegment").ToString
            IdProductLine = Session("TBListado").rows(index).Item("IdProductLine").ToString
            Dim _BusForecast = CargaParamBusqueda(IdCustomer, IdSegment, IdProductLine)

            ctrlAgregarForecast1.CargarProductoDeForecast(_BusForecast)
            Session("MostrarListado") = False
        End If
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ctrlAgregarForecast1.SoloIngreso()
        'TblListadoFore.Visible = False
        Session("MostrarListado") = False
        'MostrarListado()
    End Sub
    Private Sub CargarEntidadesUsuarios()
        _servEntidades = New ServEntidades()
        Usuario = Session("usuario")

        Dim TBEntidad = _servEntidades.SearchEntidades(Usuario.IdUsuario)  '# DEJAR AÑO PARAMETRIZADO

        DrEntidadLegalUsuario2.DataTextField = "DesEntity"
        DrEntidadLegalUsuario2.DataValueField = "IdEntityF"
        DrEntidadLegalUsuario2.DataSource = TBEntidad
        DrEntidadLegalUsuario2.DataBind()
    End Sub
    Protected Sub DrEntidadLegalUsuario2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DrEntidadLegalUsuario2.SelectedIndexChanged
        Session("IdEntidad") = DrEntidadLegalUsuario2.SelectedValue
        Usuario = Session("usuario")
        Usuario.IdEntityF = DrEntidadLegalUsuario2.SelectedValue
        Session("usuario") = Usuario

        CargarListado()
        ' Session("ActualizarListado") = True
        ' Response.Redirect("~/Formularios/listado.aspx")
    End Sub
    Protected Sub btnBuscar_Click(sender As Object, e As EventArgs) Handles btnBuscar.Click
        CargarListado()
    End Sub
    Protected Sub btnLimpiarB_Click(sender As Object, e As EventArgs) Handles btnLimpiarB.Click
        DrClientesB.SelectedValue = 0
        DrUOMB.SelectedValue = 0
        DrProductoB.SelectedValue = 0
        DrSegmentoPLB.SelectedValue = 0
        DrProductLineB.SelectedValue = 0
        TxtCodClienteB.Text = ""
    End Sub
    Private Function CargaParamBusqueda() As BusForecast

        Dim BusForecast = New BusForecast()
        BusForecast.IdCustomer = DrClientesB.SelectedValue
        BusForecast.IdUOM = DrUOMB.SelectedValue
        If DrProductoB.SelectedValue <> "" Then
            BusForecast.IdProduct = CInt(DrProductoB.SelectedValue)
        End If
        BusForecast.IdSegmento = DrSegmentoPLB.SelectedValue
        BusForecast.IdProductLine = DrProductLineB.SelectedValue
        BusForecast.CodCustomer = TxtCodClienteB.Text
        Return BusForecast

    End Function

    Public Sub CargarCombos(TipoCarga As Integer)
        _servClientes = New ServClientes()
        Dim Usuario = Session("usuario")
        Dim ListClientes = _servClientes.SearchCliente(Usuario.IdEntityF)

        DrClientesB.DataTextField = "Name"
        DrClientesB.DataValueField = "IdCustomer"
        DrClientesB.DataSource = ListClientes
        DrClientesB.DataBind()




        'Producto de Busqueda
        _servServProduct = New ServProduct()
        Dim ListProducto = _servServProduct.SearchProduct(Usuario.IdEntityF, 0, DrClientesB.SelectedValue)  '# DEJAR AÑO PARAMETRIZADO

        DrProductoB.DataTextField = "CodDesproduct"
        DrProductoB.DataValueField = "IdProduct"
        DrProductoB.DataSource = ListProducto
        DrProductoB.DataBind()

        'Cargar Solo Segemnto en Busqueda
        _servSegmentoPL = New ServSegmentPL()
        Dim ListSegmento = _servSegmentoPL.SearchSegmentPL_SoloSegmento(Usuario.IdEntityF)
        DrSegmentoPLB.DataTextField = "desSegment"
        DrSegmentoPLB.DataValueField = "IdSegment"
        DrSegmentoPLB.DataSource = ListSegmento
        DrSegmentoPLB.DataBind()

        'Cargar Solo ProductLine
        Dim ListProductLine = _servSegmentoPL.SearchSegmentPL_SoloProductLine(Usuario.IdEntityF)
        DrProductLineB.DataTextField = "DesProductLine"
        DrProductLineB.DataValueField = "IdProductLine"
        DrProductLineB.DataSource = ListProductLine
        DrProductLineB.DataBind()

        'Cargar UOM
        _servUOM = New ServUOM()
        Dim ListUOM = _servUOM.SearchUOM()
        DrUOMB.DataTextField = "DesUOM"
        DrUOMB.DataValueField = "IdUOM"
        DrUOMB.DataSource = ListUOM
        DrUOMB.DataBind()

    End Sub

    Protected Sub ChFiltro_CheckedChanged(sender As Object, e As EventArgs) Handles ChFiltro.CheckedChanged
        If ChFiltro.Checked = True Then
            TBFiltro.Visible = True
        Else
            TBFiltro.Visible = False
        End If
    End Sub


    Private Sub Listado_LoadComplete(sender As Object, e As EventArgs) Handles Me.LoadComplete
        If Session("MostrarListado") = True Then
            ctrlAgregarForecast1.Visible = False
            TblListadoFore.Visible = True
        Else
            ctrlAgregarForecast1.Visible = True
            TblListadoFore.Visible = False
        End If

    End Sub
End Class